package com.example.Laboratory.Service.Impl;

import com.example.Laboratory.Entity.News;
import com.example.Laboratory.Mapper.NewsMapper;
import com.example.Laboratory.Service.NewsService;
import com.example.Laboratory.Untils.OperationFile;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    NewsMapper newsMapper;

    //增加新闻信息
    @Override
    public String addNews(News t,MultipartFile File1,MultipartFile File2) {
        String path1 = OperationFile.upfile(File1);
        t.setNews_address1(path1);
        if (newsMapper.addNews(t) == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    //删除新闻
    @Override
    public int deleteNews(int news_id) {
        if (newsMapper.deleteNews(news_id) != 1) {
            return 0;
        } else {
            return 1;
        }
    }

    //修改新闻
    @Override
    public String reviseNews(News t,MultipartFile File1) {
        int id = t.getNews_id();
        String filepath1 = newsMapper.researchaddress1(id);
        if (filepath1.equals("没有选择文件")){
            String path1 = OperationFile.upfile(File1);
            t.setNews_address1(path1);
            if (newsMapper.reviseNews(t) == 1) {
                return "更新成功";
            } else {
                return "更新失败";
            }
        }else {
            String path1 = OperationFile.updatafile(filepath1, File1);
            t.setNews_address1(path1);
            if (newsMapper.reviseNews(t) == 1) {
                return "更新成功";
            } else {
                return "更新失败";
            }
        }
    }

    //单条查找新闻
    @Override
    public List<News> researchNews1(int news_id) {
        return newsMapper.researchNews1(news_id);
    }


    //多条查找新闻
    @Override
    public List<News> researchNews2() {
        return newsMapper.researchNews2();
    }

}
